package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

import java.util.Calendar;

@Repository("StandardDAO")
@Transactional
public class StandardDAOImpl extends AbstractJpaDao<Standard> implements StandardDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Standard.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public StandardDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Standard findStandardById(Integer id) throws DataAccessException {

		return findStandardById(id, -1, -1);
	}

	@Transactional
	public Standard findStandardById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardById", startResult, maxRows, id);
			return (Standard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Standard> findStandardByName(String name) throws DataAccessException {

		return findStandardByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStandardByName", startResult, maxRows, name);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@Transactional
	public Set<Standard> findStandardByNameContaining(String name) throws DataAccessException {

		return findStandardByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@Transactional
	public Set<Standard> findStandardByNumber(String number) throws DataAccessException {

		return findStandardByNumber(number, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByNumber(String number, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStandardByNumber", startResult, maxRows, number);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@Transactional
	public Set<Standard> findStandardByNumberContaining(String number) throws DataAccessException {

		return findStandardByNumberContaining(number, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByNumberContaining(String number, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardByNumberContaining", startResult, maxRows, number);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@Transactional
	public Set<Standard> findStandardByEffectivedate(Calendar effectivedate) throws DataAccessException {

		return findStandardByEffectivedate(effectivedate, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByEffectivedate(Calendar effectivedate, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardByEffectivedate", startResult, maxRows, effectivedate);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@Transactional
	public Set<Standard> findStandardByExpirydate(Calendar expirydate) throws DataAccessException {

		return findStandardByExpirydate(expirydate, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByExpirydate(Calendar expirydate, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardByExpirydate", startResult, maxRows, expirydate);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@Transactional
	public Set<Standard> findStandardByDownurl(String downurl) throws DataAccessException {

		return findStandardByDownurl(downurl, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByDownurl(String downurl, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardByDownurl", startResult, maxRows, downurl);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@Transactional
	public Set<Standard> findStandardByDownurlContaining(String downurl) throws DataAccessException {

		return findStandardByDownurlContaining(downurl, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByDownurlContaining(String downurl, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardByDownurlContaining", startResult, maxRows, downurl);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@Transactional
	public Set<Standard> findStandardByDescription(String description) throws DataAccessException {

		return findStandardByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@Transactional
	public Set<Standard> findStandardByDescriptionContaining(String description) throws DataAccessException {

		return findStandardByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@Transactional
	public Standard findStandardByPrimaryKey(Integer id) throws DataAccessException {
		return findStandardByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Standard findStandardByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardByPrimaryKey", startResult, maxRows, id);
			return (Standard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Standard> findStandardsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Standard> findAllStandards() throws DataAccessException {

		return findAllStandards(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findAllStandards(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllStandards", startResult, maxRows);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Standard> findStandardsByReplacedstandard(Integer standardId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllStandardsByReplacedstandard", start, max, standardId);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Standard> findStandardsByStandardtype(Integer standardtypeId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllStandardsByStandardtype", start, max, standardtypeId);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Standard> findStandardsByStandardlevel(Integer standardlevelId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllStandardsByStandardlevel", start, max, standardlevelId);
		return new LinkedHashSet<Standard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standard> findStandardByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Standard>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Standard> findStandardsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Standard.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Standard> standards = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return standards;
	}

	@Transactional
	public Standard findStandardByPhysicalPrimaryKey(String number) throws DataAccessException {
		return findStandardByPhysicalPrimaryKey(number, -1, -1);
	}

	@Transactional
	public Standard findStandardByPhysicalPrimaryKey(String number, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardByPhyscialPrimaryKey", startResult, maxRows, number);
			return (Standard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Standard entity) {
		return true;
	}
}
